package com.csw.android.player.exoplayer

import android.view.SurfaceView
import android.view.TextureView
import com.csw.android.dev_utils.utils.UUIDUtils
import com.csw.android.player.PlayerManager
import com.csw.android.player.base.*
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player

class ExoMediaPlayer : IMediaPlayer {
    private val mId = UUIDUtils.generateId()
    private val mPlayList = PlayList(this)
    private val mPlayerInfo = PlayerInfo()
    private val player = ExoplayerFactory.createSimpleExoPlayer().apply {
        ExoplayerListener(playerInfo, playList).player = this
    }

    //region IMediaPlayer---------------------------------------------------------------------------

    override val playId: String
        get() = mId

    override val playList: PlayList
        get() = mPlayList

    override val playerInfo: PlayerInfo
        get() = mPlayerInfo

    override var mediaInfo: MediaInfo? = null
        set(value) {
            if (field == value) {
                return
            }
            field = value
            playList.currMedia.value = field

            reset()
            field?.run {
                player.setMediaItem(MediaItem.fromUri(mediaUri))
                player.prepare()
            }
        }

    /**
     * 当前播放器持有者，
     */
    override var playerHolder: IPlayerHolder? = null
        set(value) {
            if (field == value) {
                return
            }
            field?.onPlayerUnbind(this@ExoMediaPlayer)
            player.setVideoSurface(null)//移除现有的画面输出
            field = value
            field?.onPlayerBind(this@ExoMediaPlayer)
        }

    override fun play() {
        player.run {
            playWhenReady = true
            if (playbackState == Player.STATE_ENDED) {
                seekTo(0)
            }
        }
    }

    override fun pause() {
        player.playWhenReady = false
    }

    override fun reset() {
        player.stop()
        player.clearMediaItems()
        playerInfo.reset()
    }

    override fun release() {
        reset()
        playerHolder = null
        mediaInfo = null
        if (this == PlayerManager.SINGLE_PLAYER) {
            return
        }
        player.release()
        PlayerManager.removePlayer(playId)
    }

    override fun isPlaying(): Boolean {
        return player.isPlaying
    }

    override fun getContentDuration(): Long {
        return player.contentDuration
    }

    override fun getContentProgress(): Long {
        return player.contentPosition
    }

    override fun setContentProgress(progress: Long) {
        player.seekTo(progress)
    }

    override fun setSurfaceView(surfaceView: SurfaceView?) {
        player.setVideoSurfaceView(surfaceView)
    }

    override fun setTextureView(textureView: TextureView?) {
        player.setVideoTextureView(textureView)
    }

    //endregion-------------------------------------------------------------------------------------
}